# coding:utf-8
import os.path

import tornado.httpserver
import tornado.options
import tornado.web
import tornado.ioloop
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from tornado.options import define, options

from handlers import front


define("port", default=9000, help="run on the given port", type=int)


class Application(tornado.web.Application):
	def __init__(self):
		handlers = [(r'/', front.IndexHandler),
					(r'/action/login', front.LoginHandler),
					(r'/login', front.LoginViewHandler),
					(r'/signup', front.SignupViewHandler),
					(r'/action/signup', front.SignupHandler)
		]
		settings = dict(
			template_path=os.path.join(os.path.dirname(__file__), "templates"),
			static_path=os.path.join(os.path.dirname(__file__), "static"),
			debug=True,
			cookie_secret="61oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=",
			login_url="/login"
		)
		# self.db = torndb.Connection("localhost:3306", "parnote_b", "root", "1234")
		DB_CONNECT_STRING = 'mysql+mysqldb://root:1234@localhost/parnote_a?charset=utf8'
		engine = create_engine(DB_CONNECT_STRING, echo=True)  # echo=True 显示SQL
		self.DB_Session = sessionmaker(bind=engine)
		tornado.web.Application.__init__(self, handlers, **settings)


def runserver():
	tornado.options.parse_command_line()
	http_server = tornado.httpserver.HTTPServer(Application())
	http_server.listen(options.port)
	tornado.ioloop.IOLoop.instance().start()


if __name__ == '__main__':
	runserver()
	# if options.cmd == 'runserver':
	# runserver()
	# elif options.cmd == 'createuser':
	# createuser()
	# elif options.cmd == 'syncdb':
	# syncdb()